package systemconfig;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;

import users.LibrarianRole;
import users.Role;
import users.UserRole;
import util.Holiday;
import exceptions.RoleNotFoundException;

public class SystemConfiguration {

    // AHORA LAS SETTEO PARA ZAFAR, DESPUES HACERLO PARSEAR

    private int maxLoansPerUser = 4;

    private int maxDaysInLoaning = 7;

    private String mailSenderAdress = "bibliotecaSarasa@gmail.com";

    private String mailSenderPassword = "123456";

    private List<Holiday> holidaysList = new ArrayList<Holiday>();

    private int minimumCommentLength = 30;

    private int maximumCommentLength = 500;

    private int daysCopyReserved = 1;

    private List<Role> rolesInApplication = new ArrayList<Role>();

    // Singleton stuff ----------------------------------------------//

    private static SystemConfiguration uniqueInstance = new SystemConfiguration();

    public static SystemConfiguration getInstance() {

        /* QUITAR ESTOS PARCHES */uniqueInstance.agregarParcheDeFeriados();
        uniqueInstance.rolesInApplication.add(UserRole.getInstance());
        uniqueInstance.rolesInApplication.add(LibrarianRole.getInstance());
        return uniqueInstance;
    }

    public List<Holiday> getHolidaysList() {
        return holidaysList;
    }

    public void setHolidaysList(final List<Holiday> holidaysList) {
        this.holidaysList = holidaysList;
    }

    public static SystemConfiguration getUniqueInstance() {
        return uniqueInstance;
    }

    public static void setUniqueInstance(final SystemConfiguration uniqueInstance) {
        SystemConfiguration.uniqueInstance = uniqueInstance;
    }

    public void setMaxLoansPerUser(final int maxLoansPerUser) {
        this.maxLoansPerUser = maxLoansPerUser;
    }

    public void setMaxDaysInLoaning(final int maxDaysInLoaning) {
        this.maxDaysInLoaning = maxDaysInLoaning;
    }

    public void setMailSenderAdress(final String mailSenderAdress) {
        this.mailSenderAdress = mailSenderAdress;
    }

    public void setMailSenderPassword(final String mailSenderPassword) {
        this.mailSenderPassword = mailSenderPassword;
    }

    public int getMaxLoansPerUser() {
        return maxLoansPerUser;
    }

    public int getMaxDaysInLoaning() {
        return maxDaysInLoaning;
    }

    public String getMailSenderAdress() {
        return mailSenderAdress;
    }

    public String getMailSenderPassword() {
        return mailSenderPassword;
    }

    public List<Holiday> getHolidayList() {
        return holidaysList;
    }

    public List<Role> getRolesInApplication() {
        return rolesInApplication;
    }

    public List<String> getRolesInApplicationAsStrings() {
        List<String> ret = new ArrayList<String>();
        for (Role it : rolesInApplication) {
            if (!ret.contains(it.toString())) {
                ret.add(it.toString());
            }
        }
        return ret;
    }

    public void setRolesInApplication(final List<Role> rolesInApplication) {
        this.rolesInApplication = rolesInApplication;
    }

    // --------DELETE ME IN THE FUTURE---------------------//
    private void agregarParcheDeFeriados() {
        holidaysList.add(new Holiday(new DateTime(2011, 07, 9, 00, 00), "Dia de la Independencia de Argentina"));
        holidaysList.add(new Holiday(new DateTime(2011, 5, 25, 00, 00), "Revolucion de Mayo Argentina"));
    }

    // --------DELETE ME IN THE FUTURE---------------------//

    public void setMinimumCommentLength(final int minimumCommentLength) {
        this.minimumCommentLength = minimumCommentLength;
    }

    public int getMinimumCommentLength() {
        return minimumCommentLength;
    }

    public void setMaximumCommentLength(final int maximumCommentLength) {
        this.maximumCommentLength = maximumCommentLength;
    }

    public int getMaximumCommentLength() {
        return maximumCommentLength;
    }

    public int getDaysCopyReserved() {
        return daysCopyReserved;
    }

    public void setDaysCopyReserved(final int daysCopyReserved) {
        this.daysCopyReserved = daysCopyReserved;
    }

    public Role getRoleByName(final String aName) {
        Role ret = null;

        for (Role it : rolesInApplication) {
            if (it.toString().equalsIgnoreCase(aName)) {
                ret = it;
            }
        }

        if (ret == null) {
            throw new RoleNotFoundException("Can not find espected role");
        }

        return ret;
    }

}
